package com.wcpdoc.survey.survey.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.wcpdoc.survey.core.dao.BaseDao;
import com.wcpdoc.survey.core.entity.Chart;
import com.wcpdoc.survey.core.entity.ChartDetail;
import com.wcpdoc.survey.core.entity.ChartType;
import com.wcpdoc.survey.core.entity.PageIn;
import com.wcpdoc.survey.core.entity.PageOut;
import com.wcpdoc.survey.core.service.impl.BaseServiceImp;
import com.wcpdoc.survey.core.util.ValidateUtil;
import com.wcpdoc.survey.survey.dao.SurveyDao;
import com.wcpdoc.survey.survey.entity.Paper;
import com.wcpdoc.survey.survey.entity.Question;
import com.wcpdoc.survey.survey.entity.QuestionEx;
import com.wcpdoc.survey.survey.entity.QuestionOption;
import com.wcpdoc.survey.survey.entity.Survey;
import com.wcpdoc.survey.survey.entity.SurveyType;
import com.wcpdoc.survey.survey.service.PaperService;
import com.wcpdoc.survey.survey.service.PaperTypeService;
import com.wcpdoc.survey.survey.service.QuestionService;
import com.wcpdoc.survey.survey.service.SurveyService;
import com.wcpdoc.survey.survey.service.SurveyTypeService;
import com.wcpdoc.survey.sys.cache.DictCache;

/**
 * 调查服务层实现
 * 
 * v1.0 zhanghc 2017-06-11 09:13:23
 */
@Service
public class SurveyServiceImpl extends BaseServiceImp<Survey> implements SurveyService {
//	private static final Logger log = LoggerFactory.getLogger(SurveyServiceImpl.class);
	
	@Resource
	private SurveyDao surveyDao;
	@Resource
	private QuestionService questionService;
	@Resource
	private PaperService paperService;
	@Resource
	private PaperTypeService paperTypeService;
	@Resource
	private SurveyTypeService surveyTypeService;
	
	@Override
	@Resource(name = "surveyDaoImpl")
	public void setDao(BaseDao<Survey> dao) {
		super.dao = dao;
	}
	

	@Override
	public void saveAndUpdate(Survey survey) {
		//校验数据有效性
		if(survey.getEndTime().getTime() <= new Date().getTime()){
			throw new RuntimeException("结束时间必须大于当前时间！");
		}
		if(survey.getStartTime().getTime() >= survey.getEndTime().getTime()){
			throw new RuntimeException("结束时间必须大于开始时间！");
		}
		
		//添加调查
		surveyDao.save(survey);
	}
	
	@Override
	public void updateAndUpdate(Survey survey) {
		//校验数据有效性
		if(survey.getState() == 1){
			throw new RuntimeException("【"+survey.getName()+"】已"+DictCache.getDictValue("SURVEY_STATE", "1")+"！" );
		}
		
		if(survey.getEndTime().getTime() <= new Date().getTime()){
			throw new RuntimeException("结束时间必须大于当前时间！");
		}
		if(survey.getStartTime().getTime() >= survey.getEndTime().getTime()){
			throw new RuntimeException("结束时间必须大于开始时间！");
		}
		
		//添加调查
		surveyDao.update(survey);
	}

	@Override
	public List<Map<String, Object>> getPaperAddListTypeTreeList() {
		return paperTypeService.getTreeList();
	}

	@Override
	public PageOut getPaperAddListpage(PageIn pageIn) {
		return paperService.getListpage(pageIn);
	}

	@Override
	public Paper getPaper(Integer paperId) {
		return paperService.getEntity(paperId);
	}

	@Override
	public List<Map<String, Object>> getSurveyTypeTreeList() {
		return surveyTypeService.getTreeList();
	}

	@Override
	public SurveyType getSurveyType(Integer id) {
		return surveyDao.getSurveyType(id);
	}

	@Override
	public List<Survey> getList(Integer surveyTypeId) {
		return surveyDao.getList(surveyTypeId);
	}

	@Override
	public void deleteAndUpdate(Integer[] ids) {
		//校验数据有效性
		if(!ValidateUtil.isValid(ids)){
			throw new RuntimeException("无法获取参数：ids");
		}
		
		//删除问题
		for(Integer id : ids){
			Survey survey = getEntity(id);
			if(survey.getState() == 1){
				throw new RuntimeException("【"+survey.getName()+"】已"+DictCache.getDictValue("SURVEY_STATE", "1")+"！" );
			}
			
			survey.setState(0);
			update(survey);
		}
	}

	@Override
	public void doPublish(Integer[] ids) {
		//校验数据有效性
		if(!ValidateUtil.isValid(ids)){
			throw new RuntimeException("无法获取参数：ids");
		}
		
		//发布
		for(Integer id : ids){
			Survey survey = getEntity(id);
			if(survey.getState() == 0){
				throw new RuntimeException("【"+survey.getName()+"】已"+DictCache.getDictValue("SURVEY_STATE", "0")+"！" );
			}
			if(survey.getState() == 1){
				throw new RuntimeException("【"+survey.getName()+"】已"+DictCache.getDictValue("SURVEY_STATE", "1")+"！" );
			}
			if(survey.getState() == 2){
				survey.setState(1);
				update(survey);
			}
		}
	}


	@Override
	public void doSurveyTypeUpdate(Integer[] ids, Integer surveyTypeId) {
		//校验数据有效性
		if(!ValidateUtil.isValid(ids)){
			throw new RuntimeException("无法获取参数：ids");
		}
		if(surveyTypeId == null){
			throw new RuntimeException("无法获取参数：surveyTypeId");
		}
		
		//完成设置调查
		for(Integer id : ids){
			Survey survey = getEntity(id);
			if(survey.getSurveyTypeId() == surveyTypeId){
				continue;
			}
			
			survey.setSurveyTypeId(surveyTypeId);
			surveyDao.update(survey);
		}
	}

	@Override
	public Chart getChart(Integer surveyId, Integer questionId) {
		Chart chart = new Chart();
		chart.setTitle("统计分析");
		
		Question question = questionService.getEntity(questionId);
		QuestionEx questionEx = new QuestionEx(question);
		Map<String, Object> chartData = surveyDao.getChartData(surveyId, questionId);
		if (questionEx.getType() == 1 || questionEx.getType() == 2 || questionEx.getType() == 4 
				|| questionEx.getType() == 5 || questionEx.getType() == 7) {
			List<ChartDetail> chartDetailList = new ArrayList<ChartDetail>();
			List<QuestionOption> rowOptionList = questionEx.getRowOptionList();
			for(QuestionOption rowOption : rowOptionList){
				ChartDetail chartDetail = new ChartDetail();
				chartDetail.setName(rowOption.getName());
				chartDetail.setCount(((BigDecimal)chartData.get(rowOption.getIndex())).intValue());
				chartDetailList.add(chartDetail);
			}
			chart.setChartDetailList(chartDetailList);
		}else if(questionEx.getType() == 3 || questionEx.getType() == 6){
			List<ChartType> chartTypeList = new ArrayList<ChartType>();
			List<QuestionOption> rowOptionList = questionEx.getRowOptionList();
			for(QuestionOption rowOption : rowOptionList){
				ChartType chartType = new ChartType();
				chartType.setName(rowOption.getName());
				
				List<ChartDetail> chartDetailList = new ArrayList<ChartDetail>();
				List<QuestionOption> colOptionList = questionEx.getColOptionList();
				for(QuestionOption colOption : colOptionList){
					ChartDetail chartDetail = new ChartDetail();
					chartDetail.setName(colOption.getName());
					chartDetail.setCount(((BigDecimal)chartData.get(rowOption.getIndex() + colOption.getIndex())).intValue());
					chartDetailList.add(chartDetail);
				}

				chartType.setChartDetailList(chartDetailList);
				chartTypeList.add(chartType);
			}
			chart.setChartTypeList(chartTypeList);
		}else if(questionEx.getType() == 8){
			List<ChartType> chartTypeList = new ArrayList<ChartType>();
			List<QuestionOption> rowOptionList = questionEx.getRowOptionList();
			for(QuestionOption rowOption : rowOptionList){
				ChartType chartType = new ChartType();
				chartType.setName(rowOption.getName());
				chartType.setChartTypeList(new ArrayList<ChartType>());
				
				List<ChartType> chartTypeList2 = chartType.getChartTypeList();
				List<QuestionOption> colOptionList = questionEx.getColOptionList();
				for(QuestionOption colOption : colOptionList){
					ChartType chartType2 = new ChartType();
					chartType2.setName(colOption.getName());
					List<QuestionOption> selectOptionList = questionEx.getSelectOptionList();
					List<ChartDetail> chartDetailList = new ArrayList<ChartDetail>();
					for(QuestionOption selectOption : selectOptionList){
						ChartDetail chartDetail = new ChartDetail();
						chartDetail.setName(selectOption.getName());
						chartDetail.setCount(((BigDecimal)chartData.get(rowOption.getIndex() + colOption.getIndex() + selectOption.getIndex())).intValue());
						chartDetailList.add(chartDetail);
					}
					chartType2.setChartDetailList(chartDetailList);
					
					chartTypeList2.add(chartType2);
				}
				chartType.setChartTypeList(chartTypeList2);
				chartTypeList.add(chartType);
			}
			chart.setChartTypeList(chartTypeList);
		}
		return chart;
	}
}
